package com.youlai.quarkus.system.converter;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.quarkus.system.model.entity.SysDict;
import com.youlai.quarkus.system.model.form.DictForm;
import com.youlai.quarkus.system.model.vo.DictPageVO;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 字典数据项对象转换器
 *
 * @author haoxr
 * @since 2022/6/8
 */
@ApplicationScoped
public class DictConverter {

    public Page<DictPageVO> entity2Page(Page<SysDict> page) {
        if (page == null) {
            return null;
        } else {
            Page<DictPageVO> page1 = new Page();
            page1.setPages(page.getPages());
            page1.setRecords(this.sysDictListToDictPageVOList(page.getRecords()));
            page1.setTotal(page.getTotal());
            page1.setSize(page.getSize());
            page1.setCurrent(page.getCurrent());
            page1.setSearchCount(page.searchCount());
            page1.setOptimizeCountSql(page.optimizeCountSql());
            List<OrderItem> list1 = page.orders();
            if (list1 != null) {
                page1.setOrders(new ArrayList(list1));
            }

            page1.setMaxLimit(page.maxLimit());
            page1.setCountId(page.countId());
            return page1;
        }
    }

    public DictForm entity2Form(SysDict entity) {
        if (entity == null) {
            return null;
        } else {
            DictForm dictForm = new DictForm();
            dictForm.setId(entity.getId());
            dictForm.setTypeCode(entity.getTypeCode());
            dictForm.setName(entity.getName());
            dictForm.setValue(entity.getValue());
            dictForm.setStatus(entity.getStatus());
            dictForm.setSort(entity.getSort());
            dictForm.setRemark(entity.getRemark());
            return dictForm;
        }
    }

    public SysDict form2Entity(DictForm entity) {
        if (entity == null) {
            return null;
        } else {
            SysDict sysDict = new SysDict();
            sysDict.setId(entity.getId());
            sysDict.setTypeCode(entity.getTypeCode());
            sysDict.setName(entity.getName());
            sysDict.setValue(entity.getValue());
            sysDict.setSort(entity.getSort());
            sysDict.setStatus(entity.getStatus());
            sysDict.setRemark(entity.getRemark());
            return sysDict;
        }
    }

    protected DictPageVO sysDictToDictPageVO(SysDict sysDict) {
        if (sysDict == null) {
            return null;
        } else {
            DictPageVO dictPageVO = new DictPageVO();
            dictPageVO.setId(sysDict.getId());
            dictPageVO.setName(sysDict.getName());
            dictPageVO.setValue(sysDict.getValue());
            dictPageVO.setStatus(sysDict.getStatus());
            return dictPageVO;
        }
    }

    protected List<DictPageVO> sysDictListToDictPageVOList(List<SysDict> list) {
        if (list == null) {
            return null;
        } else {
            List<DictPageVO> list1 = new ArrayList(list.size());
            Iterator var3 = list.iterator();

            while(var3.hasNext()) {
                SysDict sysDict = (SysDict)var3.next();
                list1.add(this.sysDictToDictPageVO(sysDict));
            }

            return list1;
        }
    }

}
